package cc.magicjson.minigame.demo.ddd.domain.model;

import lombok.Getter;

import java.time.LocalDateTime;

@Getter
public class Game {
	private final GameId id;
	private final GameName name;
	private GameStatus status;
	private LocalDateTime startTime;
	private LocalDateTime endTime;
	private int playerCount;

	public Game(GameId id, GameName name) {
		this.id = id;
		this.name = name;
		this.status = GameStatus.CREATED;
		this.playerCount = 0;
	}

	public void start() {
		if (status != GameStatus.CREATED) {
			throw new IllegalStateException("Game can only be started when in CREATED state");
		}
//		if (playerCount < 2) {
//			throw new IllegalStateException("Game must have at least 2 players to start");
//		}
		status = GameStatus.IN_PROGRESS;
		startTime = LocalDateTime.now();
	}

	public void end() {
		if (status != GameStatus.IN_PROGRESS) {
			throw new IllegalStateException("Game can only be ended when in IN_PROGRESS state");
		}
		status = GameStatus.ENDED;
		endTime = LocalDateTime.now();
	}

	public void addPlayer() {
		if (status != GameStatus.CREATED) {
			throw new IllegalStateException("Players can only be added when game is in CREATED state");
		}
		if (playerCount >= 4) {
			throw new IllegalStateException("Game cannot have more than 4 players");
		}
		playerCount++;
	}

	public void removePlayer() {
		if (status != GameStatus.CREATED) {
			throw new IllegalStateException("Players can only be removed when game is in CREATED state");
		}
		if (playerCount <= 0) {
			throw new IllegalStateException("No players to remove");
		}
		playerCount--;
	}

	public enum GameStatus {
		CREATED, IN_PROGRESS, ENDED
	}
}